/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：FriendOperationScreen.java <br>
 * 创建时间：2014-8-2 下午8:10:35 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.community;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobACL;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.DeleteListener;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.data.Npc;
import lostland.gmud.exv2.data.Room;
import lostland.gmud.exv2.expand.GeneralMenuScreen;
import lostland.gmud.exv2.ui.LockedScreen;
import lostland.gmud.exv2.ui.NotificationScreen;


/**
 * 类名：FriendOperationScreen <br>
 * 说明：
 *
 * @author 12548
 */
public class FriendOperationScreen extends GeneralMenuScreen {

	GmudUser friend;

	public FriendOperationScreen(GmudUser s) {
		super(gets(), "请选择对好友:" + s.getNickName() + "的操作");
		friend = s;
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ScrollableMenuScreen#onCancel()
	 */
	@Override
	public void onCancel() {
		getGame().popScreen();
	}

	/* （非 Javadoc）
	 * @see lostland.gmud.exv2.ui.core.ScrollableMenuScreen#onClick(int)
	 */
	@Override
	public void onClick(int i) {
		FriendList fl = FriendList.getInstance();

		switch (i) {

			case 0:
				bindCharactor();
				break;
			case 1:
				new LockedScreen("正在联网......").pushToGame();
				BmobQuery<MarriageProposing> q = new BmobQuery<>();
				q.addWhereEqualTo("sender", GmudUser.getInstance());
				q.addWhereEqualTo("receiver", friend);

				q.findObjects(getGame(), new FindListener<MarriageProposing>() {

					@Override
					public void onError(int arg0, String arg1) {
						new NotificationScreen("联网错误：" + arg1).replaceCurrent();
					}

					@Override
					public void onSuccess(List<MarriageProposing> arg0) {

						if (Game.getData().spouse != null) {
							new NotificationScreen("你已经有了配偶！").replaceCurrent();
						} else if (arg0.size() > 0)
							new NotificationScreen("求婚信息已存在！").replaceCurrent();
						else {

							MarriageProposing mp = new MarriageProposing();
							mp.setSender(GmudUser.getInstance());
							mp.setReceiver(friend);
							mp.setStat(0);

							BmobACL acl = new BmobACL();

							acl.setPublicWriteAccess(false);
							acl.setPublicReadAccess(false);

							acl.setReadAccess(mp.getSender(), true);
							acl.setReadAccess(mp.getReceiver(), true);
							acl.setWriteAccess(mp.getSender(), true);
							acl.setWriteAccess(mp.getReceiver(), true);

							mp.setACL(acl);

							mp.save(getGame(), new SaveListener() {
								@Override
								public void onFailure(int arg0, String arg1) {
									new NotificationScreen("联网错误：" + arg1).replaceCurrent();
								}

								@Override
								public void onSuccess() {
									new NotificationScreen("求婚信息发送成功，请等待对方回应！").replaceCurrent();
								}
							});
						}


					}
				});

				break;
			case 2:


				fl.friends.remove(friend);
				new LockedScreen("正在更新好友列表......").pushToGame();


				FriendShip ship = null;

				for (FriendShip t : fl.ships) {
					if (t.getOther().equals(friend))
						ship = t;
				}

				if (ship != null)
					ship.delete(getGame(), new DeleteListener() {

						@Override
						public void onFailure(int arg0, String arg1) {
							new NotificationScreen("联网错误：" + arg1).replaceCurrent();
						}

						@Override
						public void onSuccess() {
							new NotificationScreen("删除好友成功！").replaceCurrent();
						}
					});
				else
					getGame().popScreen();
				break;
		}
	}

	/**
	 *
	 */
	protected void bindCharactor() {
		new LockedScreen("正在查询好友信息......").pushToGame();

		BmobQuery<BindingCharactor> q = new BmobQuery<>();
		q.addWhereEqualTo("owner", friend);

		q.findObjects(getGame(), new FindListener<BindingCharactor>() {

			@Override
			public void onError(int arg0, String arg1) {
				new NotificationScreen("好友信息获取失败！").replaceCurrent();
			}

			@Override
			public void onSuccess(final List<BindingCharactor> arg0) {
				if (arg0.size() == 0) {
					new NotificationScreen("该好友尚未绑定角色！").replaceCurrent();
				} else {
					Game.getInstance().popScreen();

					int current_gold = GmudUser.getInstance().getGold();

					if (current_gold < 100) {
						new NotificationScreen("你的社区金币不足！").pushToGame();
					} else {
						new Room().npc(arg0.get(0).getCharactor())
								.introduction("")
								.flag(Npc.FLAG_NODROP)
								.flag(Npc.FLAG_IMPORTANT)
								.look(-1, -1)
								.say("请指教！")
								.place(12, 5, 3);

						new NotificationScreen("对方已出现在武馆，请前往武馆与其切磋！").pushToGame();

					}

				}

			}
		});
	}

	private static ArrayList<String> gets() {
		ArrayList<String> s = new ArrayList<>();

//		s.add("查看资料（尚未开放）");
		s.add("和TA切磋");
		s.add("向TA求婚");
		s.add("删除好友");
		return s;
	}
}
